package com.southminority.ethnic.controller.member;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.member.MemberService;
import com.southminority.ethnic.controller.member.vo.MemberVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 团队成员展示接口
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "08 - 团队成员展示API", description = "【前台】团队成员信息查询与展示相关接口")
@RestController
@RequestMapping("/api/team/member")
@CrossOrigin
public class MemberController {

    private final MemberService memberService;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有已启用的团队成员列表", description = "用于前端团队介绍页面，结果默认按排序值升序、创建时间降序排列。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = R.class))),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<MemberVO>> getMemberList(
            @Parameter(description = "归属机构ID", example = "1") @RequestParam(required = false) Long organizationId) {
        try {
            List<MemberVO> memberList = memberService.getAllMembersByOrganization(organizationId);
            log.info("获取团队成员列表成功，共{}位成员", memberList.size());
            return R.ok(memberList);
        } catch (Exception e) {
            log.error("获取团队成员列表失败", e);
            return R.fail("获取团队成员列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/by-organization/{organizationId}")
    @Operation(summary = "根据机构ID获取团队成员列表", description = "当需要严格限制某个机构时，可以直接使用该接口。")
    public R<List<MemberVO>> getMemberListByOrganization(
            @Parameter(description = "机构ID") @PathVariable Long organizationId) {
        try {
            List<MemberVO> memberList = memberService.getAllMembersByOrganization(organizationId);
            log.info("根据机构获取团队成员列表成功，orgId: {}，共{}位成员", organizationId, memberList.size());
            return R.ok(memberList);
        } catch (Exception e) {
            log.error("根据机构获取团队成员列表失败，orgId: {}", organizationId, e);
            return R.fail("获取团队成员列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单个团队成员的详细信息", description = "根据成员ID查询，返回其完整的公开信息。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "团队成员不存在或已被禁用"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<MemberVO> getMemberDetail(@Parameter(description = "成员的唯一ID", example = "1", required = true) @PathVariable Long id) {
        try {
            MemberVO memberVO = memberService.getMemberDetail(id);
            log.info("获取团队成员详情成功，成员ID: {}", id);
            return R.ok(memberVO);
        } catch (Exception e) {
            log.error("获取团队成员详情失败，成员ID: {}", id, e);
            return R.fail("获取团队成员详情失败: " + e.getMessage());
        }
    }

    @GetMapping("/departments")
    @Operation(summary = "获取所有不重复的部门列表", description = "用于前端筛选菜单，返回一个包含所有部门名称的字符串列表。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<String>> getDepartments() {
        try {
            List<String> departments = memberService.getDepartments();
            return R.ok(departments);
        } catch (Exception e) {
            log.error("获取部门列表失败", e);
            return R.fail("获取部门列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/titles")
    @Operation(summary = "获取所有不重复的职称列表", description = "用于前端筛选菜单，返回一个包含所有职称名称的字符串列表。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<String>> getTitles() {
        try {
            List<String> titles = memberService.getTitles();
            return R.ok(titles);
        } catch (Exception e) {
            log.error("获取职称列表失败", e);
            return R.fail("获取职称列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性", description = "一个简单的GET请求，用于检查服务是否正常运行。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "服务正常")
    })
    public R<String> test() {
        return R.ok("团队成员服务正常");
    }
} 
